import _ from 'lodash';

const getCommonFieldColumns = instance => {
  return [
    {
      fieldType: 'select-dictionary',
      fieldName: 'belongFieldCode',
      label: '所属领域：',
      col: { span: 8 },
      props: {
        job: '所属领域',
        optionValue: 'dictCode',
        disabled: true,
      },
      validate: [],
    },
    {
      fieldType: 'select-dictionary',
      fieldName: 'categoryCode',
      label: '所属类别：',
      col: { span: 8 },
      props: { pid: 53 },
      onSelectItem(obj) {
        instance.$refs['dynamicFormRef'] && instance.$refs['dynamicFormRef'].resetFields();
        obj.form.categoryCode = obj.record.dictCode;
      },
      validate: [{ required: true, message: '所属类别不能为空', trigger: 'change' }],
    },
    {
      fieldType: 'select-dictionary',
      fieldName: 'unitTypeCode',
      label: '单位类型：',
      col: { span: 8 },
      visible: form => form.categoryCode === '13',
      props: { code: 'unitType' },
      validate: [{ required: true, message: '单位类型不能为空', trigger: 'change' }],
    },
    {
      fieldType: 'select-dictionary',
      fieldName: 'hospitalGradeCode',
      label: '医院等级：',
      col: { span: 8 },
      visible: form => form.categoryCode === '4',
      props: { code: 'hospitalGrade' },
      validate: [{ required: true, message: '医院等级不能为空', trigger: 'change' }],
    },

    {
      fieldType: 'input',
      fieldName: 'riskUnitName',
      label: '风险单元名称：',
      col: { span: 8 },
      props: { maxlength: 50, placeholder: '请输入' },
      validate: [{ required: true, message: '风险单元名称不能为空', trigger: 'blur' }],
    },
    {
      fieldType: 'select-dictionary',
      fieldName: 'directorUnitCode',
      label: '行业主管单位：',
      col: { span: 8 },
      props: { job: '单位' },
      validate: [{ required: true, message: '行业主管单位不能为空', trigger: 'change' }],
    },
    {
      fieldType: 'select-dictionary',
      fieldName: 'riskAreaCode',
      label: '行政区域：',
      col: { span: 8 },
      props: { job: '区域' },
      validate: [{ required: true, message: '行政区域不能为空', trigger: 'change' }],
      onSelectItem(obj) {
        instance.$refs.centerMap && instance.$refs.centerMap.location(obj.record ? obj.record.gisId : '');
      },
    },

    {
      fieldType: 'input',
      fieldName: 'riskAddress',
      label: '地理位置：',
      col: { span: 8 },
      props: { maxlength: 100, placeholder: '请输入详细地址' },
      validate: [{ required: true, message: '地理位置不能为空', trigger: 'blur' }],
    },
    {
      fieldType: 'input',
      fieldName: 'longitude',
      label: '经度：',
      col: { span: 8 },
      props: { placeholder: '请输入经度' },
      validate: [
        { required: true, message: '经度不能为空', trigger: 'blur' },
        { pattern: /^-?\d{1,3}(\.\d{0,6})?$/, message: '经度最多输入3位整数,6位小数' },
      ],
      on(obj) {
        instance.$refs.centerMap && instance.$refs.centerMap.addPoint(obj.form.longitude, obj.form.latitude, true);
      },
    },
    {
      fieldType: 'input',
      fieldName: 'latitude',
      label: '纬度：',
      col: { span: 8 },
      props: { placeholder: '请输入纬度' },
      validate: [
        { required: true, message: '纬度不能为空', trigger: 'blur' },
        { pattern: /^-?\d{1,2}(\.\d{0,6})?$/, message: '纬度最多输入2位整数,6位小数' },
      ],
      on(obj) {
        instance.$refs.centerMap && instance.$refs.centerMap.addPoint(obj.form.longitude, obj.form.latitude, true);
      },
    },

    {
      fieldType: 'input',
      fieldName: 'contactPerson',
      label: '联系人：',
      col: { span: 8 },
      props: { placeholder: '请输入联系人', maxlength: 50 },
    },
    {
      fieldType: 'input',
      fieldName: 'contactNumber',
      label: '联系方式：',
      col: { span: 8 },
      props: { placeholder: '请输入联系方式', maxlength: 50 },
      validate: [{ pattern: /^[1][345789][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }],
    },
  ];
};

const getFieldColumnsByCategoryCode = (instance, categoryCode, isDisabled) => {
  return getCommonFieldColumns(instance).map(item => {
    if (!_.isBoolean(item.props.disabled) && _.isBoolean(isDisabled)) {
      item.props.disabled = isDisabled;
    }
    return item;
  });
};

export default getFieldColumnsByCategoryCode;
